package com.baomidou.mybatisplus.samples.quickstart.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.samples.quickstart.entity.SysUser;
import com.baomidou.mybatisplus.samples.quickstart.entity.condition.SysUserCondition;
import com.baomidou.mybatisplus.samples.quickstart.service.SysUserService;
import com.example.entity.MicroPage;
import com.example.entity.MicroPageRequest;
import com.example.entity.MicroResponse;
import com.example.util.MicroResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.locks.Condition;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 03:17
 **/

@Controller
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @GetMapping("/hello")
    public ResponseEntity hello(){
        System.out.println("Hello World");
        return ResponseEntity.ok(MicroResponseUtils.success("Hello World"));
    }

    @GetMapping("/selectall")
    public ResponseEntity getAllUsers() {
//        return ResponseEntity.ok(userService.findAll());
        return ResponseEntity.ok(MicroResponseUtils.success(userService.findAll()));
    }

    @GetMapping("/select")
    public ResponseEntity getUserById(@RequestParam Long id) {
        Optional<SysUser> user = userService.findById(id);
        ResponseEntity responseEntity;
        if(user.isPresent()){
//            responseEntity = new ResponseEntity<>(user.get(), HttpStatus.OK);
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(user.get()));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }

    @PostMapping("/create")
    public ResponseEntity createUser(@RequestBody SysUser user) {
        SysUser createdUser = userService.isave(user);
        return ResponseEntity.ok(MicroResponseUtils.success(createdUser));
    }

    @PostMapping("/update")
    public ResponseEntity updateUser(@RequestBody SysUser user) {
        Optional<SysUser> update = userService.update(user);
        if (update.isPresent()) {
            return ResponseEntity.ok(MicroResponseUtils.success(update.get()));
        } else {
            return ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
    }

    @PostMapping("/delete")
    public ResponseEntity deleteUser(@RequestParam Long id) {
        boolean isDeleted = userService.delete(id);
        return isDeleted ? ResponseEntity.ok(MicroResponseUtils.success("User deleted successfully")) : ResponseEntity.ok(MicroResponseUtils.error("User not found"));
    }

    // 增加分页功能
    @PostMapping("/page")
    public ResponseEntity page(@RequestParam Integer current, @RequestParam Integer size, @RequestBody SysUserCondition condition) {
        MicroPage<SysUser> page = userService.page(current, size, condition);
        return ResponseEntity.ok(MicroResponseUtils.success(page));
    }

    @PostMapping("page/sorted")
    public ResponseEntity page2(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestBody SysUserCondition condition,
            @RequestParam(required = false, defaultValue = "age") String sortedFiled,
            @RequestParam(required = false, defaultValue = "asc") String sortedOrder
            ) {
        MicroPage<SysUser> page2 = userService.page2(current, size, condition, sortedFiled, sortedOrder);
        return ResponseEntity.ok(MicroResponseUtils.success(page2));
    }

}
